﻿using AiShow.Commen.Encryption;
using AiShow.Model;
using AiShow.Model.OperResult;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiShow.IDAL;
using AiShow.BLL;

namespace AiShow.BLL
{
    public class AccountManager : IAccountManager
    {
        /// <summary>
        /// 通过用户名登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns>返回用户信息</returns>
        public OperResult<TBUsers> LoginByUserName(string userName, string password)
        {
            if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password))
            {
                return OperResult<TBUsers>.Failed("用户名和密码不能为空！");
            }
            // 通过用户名查询用户
            IAccountService userService = new AccountService();
            TBLogins loginEntity = userService.FindByUserName(userName);
            if (loginEntity == null)
            {
                return OperResult<TBUsers>.Failed("用户名不存在！");
            }
            // 验证用户密码
            SHAEncryption shaEncryption = new SHAEncryption();
            if (!shaEncryption.SHA1Encrypt(password).Equals(loginEntity.Password))
            {
                return OperResult<TBUsers>.Failed("用户名或密码错误！");
            }
            // 查找用户信息
            IUserService profileService = new UserService();
            TBUsers profileEntity = profileService.FindByUserId(loginEntity.Id);
            // 返回结果
            return OperResult<TBUsers>.Succeed(profileEntity);
        }

        /// <summary>
        /// 通过手机号登录
        /// </summary>
        /// <param name="mobileNumber"></param>
        /// <param name="password"></param>
        /// <returns>返回用户信息</returns>
        public OperResult<TBUsers> LoginByMobileNumber(string mobileNumber, string password)
        {
            if (string.IsNullOrWhiteSpace(mobileNumber) || string.IsNullOrWhiteSpace(password))
            {
                return OperResult<TBUsers>.Failed("手机号和密码不能为空！");
            }
            // 通过用户名查询用户
            IAccountService userService = new AccountService();
            TBLogins loginEntity = userService.FindByMobileNumber(mobileNumber);
            if (loginEntity == null)
            {
                return OperResult<TBUsers>.Failed("手机号未注册！");
            }
            // 验证用户密码
            SHAEncryption shaEncryption = new SHAEncryption();
            if (!shaEncryption.SHA1Encrypt(password).Equals(loginEntity.Password))
            {
                return OperResult<TBUsers>.Failed("手机号或密码错误！");
            }
            // 查找用户信息
            IUserService profileService = new UserService();
            TBUsers profileEntity = profileService.FindByUserId(loginEntity.Id);
            // 返回结果
            return OperResult<TBUsers>.Succeed(profileEntity);
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="users">登录信息</param>
        /// <returns></returns>
        public OperResult Register(TBLogins login)
        {
            if (string.IsNullOrWhiteSpace(login.UserName))
            {
                return OperResult.Failed("用户名不能为空！");
            }
            if (string.IsNullOrWhiteSpace(login.MobileNumber))
            {
                return OperResult.Failed("手机号不能为空！");
            }
            if (string.IsNullOrWhiteSpace(login.Password))
            {
                return OperResult.Failed("密码不能为空！");
            }
            // 判断用户名是否已注册
            IAccountService userService = new AccountService();
            if (userService.ExistUserName(login.UserName))
            {
                return OperResult.Failed("用户名已注册！");
            }
            // 判断手机号是否已注册
            if (userService.ExistMobileNumber(login.MobileNumber))
            {
                return OperResult.Failed("手机号已注册！");
            }
            // 加密用户密码
            SHAEncryption shaEncryption = new SHAEncryption();
            login.Password = shaEncryption.SHA1Encrypt(login.Password);
            // 注册用户
            try
            {
                userService.Register(login);
            }
            catch (Exception ex)
            {
                return OperResult.Wrong(ex);
            }
            // 返回结果
            return OperResult.Succeed();
        }
    }
}

